package trim.gui.testing.components;

/**
 * Создан 08.11.2009 18:35:37
 */
import java.awt.Font;
import java.awt.GraphicsEnvironment;

public class FontChooserPanel extends javax.swing.JPanel
{
    private boolean bold = false;
    private boolean italic = false;
    private Font selectedFont;

    /** Creates new form FontChooser */
    public FontChooserPanel()
    {
        initComponents();

        String [] fonts = GraphicsEnvironment.
                getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

        if (fonts.length > 0)
        {
            jFontList.setListData(fonts);
            jFontList.setSelectedIndex(0);
        }
    }

    private void initComponents()
    {
        jPreviewLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jFontList = new javax.swing.JList();
        jSettingsPanel = new javax.swing.JPanel();
        jSizeLabel = new javax.swing.JLabel();
        jFontSizeComboBox = new javax.swing.JComboBox();
        jBoldCheckBox = new javax.swing.JCheckBox();
        jItalicCheckBox = new javax.swing.JCheckBox();

        setLayout(new java.awt.BorderLayout());

        jPreviewLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jPreviewLabel.setText("Aa Bb Cc");
        jPreviewLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        add(jPreviewLabel, java.awt.BorderLayout.SOUTH);

        jFontList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jFontListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jFontList);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jSizeLabel.setText("Size:");
        jSettingsPanel.add(jSizeLabel);

        jFontSizeComboBox.setEditable(true);
        jFontSizeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "6", "7", "8", "9", "10", "12", "14", "16", "18", "20", "22", "24", "28", "32", "36", "48", "54", "60", "72" }));
        jFontSizeComboBox.setSelectedIndex(7);
        jFontSizeComboBox.setMinimumSize(new java.awt.Dimension(70, 24));
        jFontSizeComboBox.setPreferredSize(new java.awt.Dimension(70, 24));
        jFontSizeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFontSizeComboBoxActionPerformed(evt);
            }
        });
        jSettingsPanel.add(jFontSizeComboBox);

        jBoldCheckBox.setText("Bold");
        jBoldCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jBoldCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jBoldCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBoldCheckBoxActionPerformed(evt);
            }
        });
        jSettingsPanel.add(jBoldCheckBox);

        jItalicCheckBox.setText("Italic");
        jItalicCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jItalicCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jItalicCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jItalicCheckBoxActionPerformed(evt);
            }
        });
        jSettingsPanel.add(jItalicCheckBox);

        add(jSettingsPanel, java.awt.BorderLayout.PAGE_START);
    }

    private void jItalicCheckBoxActionPerformed(java.awt.event.ActionEvent evt)
    {
        italic = jItalicCheckBox.isSelected();
        changeFont();
    }

    private void jBoldCheckBoxActionPerformed(java.awt.event.ActionEvent evt)
    {
        bold = jBoldCheckBox.isSelected();
        changeFont();
    }

    private void jFontListValueChanged(javax.swing.event.ListSelectionEvent evt)
    {
        changeFont();
    }

    private void jFontSizeComboBoxActionPerformed(java.awt.event.ActionEvent evt)
    {
        changeFont();
    }

    private void changeFont()
    {
        try
        {
            String fontName = (String) jFontList.getSelectedValue();
            int fontSize = Integer.parseInt((String) jFontSizeComboBox.getSelectedItem());

            int style = Font.PLAIN;

            if (bold) style = style | Font.BOLD;
            if (italic) style = style | Font.ITALIC;

            if ( (fontName != null) && (fontName.length() > 0))
            {
                selectedFont = new Font(fontName, style, fontSize);
                jPreviewLabel.setFont(selectedFont);
            }
        }
        catch (NumberFormatException ex)
        {
            ex.printStackTrace();
        }
    }

    public Font getSelectedFont()
    {
        return selectedFont;
    }

    private javax.swing.JCheckBox jBoldCheckBox;
    private javax.swing.JList jFontList;
    private javax.swing.JComboBox jFontSizeComboBox;
    private javax.swing.JCheckBox jItalicCheckBox;
    private javax.swing.JLabel jPreviewLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel jSettingsPanel;
    private javax.swing.JLabel jSizeLabel;
}